package com.library.admin.form;

import books.storage.service.AuthorDto;
import books.storage.service.LiteraryGenreDto;

import com.library.admin.listener.AddBookListener;
import com.library.admin.service.LibraryService;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Validator;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.TextField;

public class AddBookForm extends Form {
	private static final long serialVersionUID = -23738169089505515L;
	
	public static final String AUTHOR = "author";
	public static final String AUTHOR_NAME = "author_name";

	public static final String GENRE = "genre";
	public static final String GENRE_NAME = "genre_name";
	
	private TextField 	bookTitleTextField;
	private TextField 	bookISBNTextField;
	private ComboBox 	bookAuthorComboBox;
	private ComboBox 	bookGenreComboBox;
	private TextField 	bookEditionDateTextField;
	private TextField 	bookCopiesTextField;
	private Button 		submitButton;

	public AddBookForm() {
		init();
	}

	private void init() {
		setWidth("100%");
		setImmediate(true);
		setValidationVisible(false);
		setValidationVisibleOnCommit(false);
		
		bookTitleTextField = new TextField("Nom");
		bookISBNTextField = new TextField("ISBN");
		bookAuthorComboBox = new ComboBox("Auteur");
		bookGenreComboBox = new ComboBox("Genre");
		bookEditionDateTextField = new TextField("Date d'édition (yyyy-mm-dd)");
		bookCopiesTextField = new TextField("Nb exemplaires");
		
		//Creating authors container
		Container authorContainer = new IndexedContainer();
		authorContainer.addContainerProperty(AUTHOR_NAME, 	String.class, 		null);
		authorContainer.addContainerProperty(AUTHOR, 		AuthorDto.class, 	null);
		for (AuthorDto author : LibraryService.getServiceAuthor().findAllAuthor()) {
			Item authorItem = authorContainer.addItem(author.getId());
			
			authorItem.getItemProperty(AUTHOR_NAME).setValue(author.getName());
			authorItem.getItemProperty(AUTHOR).setValue(author);
		}
		
		//Creating genres container
		Container genreContainer = new IndexedContainer();
		genreContainer.addContainerProperty(GENRE_NAME,	String.class, 				null);
		genreContainer.addContainerProperty(GENRE, 		LiteraryGenreDto.class, 	null);
		for (LiteraryGenreDto genre : LibraryService.getServiceBook().findAllLiteraryGenre()) {
			Item genreItem = genreContainer.addItem(genre.getId());
			
			genreItem.getItemProperty(GENRE_NAME).setValue(genre.getWording());
			genreItem.getItemProperty(GENRE).setValue(genre);
		}
		
		bookAuthorComboBox.setItemCaptionPropertyId(AUTHOR_NAME);
		bookAuthorComboBox.setContainerDataSource(authorContainer);
		
		bookGenreComboBox.setItemCaptionPropertyId(GENRE_NAME);
		bookGenreComboBox.setContainerDataSource(genreContainer);
		
		submitButton = new Button("Ajouter");
		submitButton.addListener(new AddBookListener(this));
		
		addField("bookTitleTextField", bookTitleTextField);
		addField("bookISBNTextField", bookISBNTextField);
		addField("bookAuthorComboBox", bookAuthorComboBox);
		addField("bookGenreTextField", bookGenreComboBox);
		addField("bookEditionDateTextField", bookEditionDateTextField);
		addField("bookCopiesTextField", bookCopiesTextField);
		addField("submitButton", submitButton);
		
		Validator isbnValidator = new RegexpValidator("\\d{13}", "Le numéro ISBN doit comporter 13 chiffres.");
		
		getField("bookTitleTextField").setRequired(true);
		getField("bookTitleTextField").setRequiredError("Le champ \"Nom\" est obligatoire");
		getField("bookISBNTextField").setRequired(true);
		getField("bookISBNTextField").setRequiredError("Le champ \"ISBN\" est obligatoire");
		getField("bookISBNTextField").addValidator(isbnValidator);
		getField("bookEditionDateTextField").setRequired(true);
		getField("bookEditionDateTextField").setRequiredError("Le champ \"Date d'édition\" est obligatoire");
	}

	public TextField getBookTitleTextField() {
		return bookTitleTextField;
	}

	public TextField getBookISBNTextField() {
		return bookISBNTextField;
	}

	public ComboBox getBookAuthorComboBox() {
		return bookAuthorComboBox;
	}

	public ComboBox getBookGenreComboBox() {
		return bookGenreComboBox;
	}

	public TextField getBookEditionDateTextField() {
		return bookEditionDateTextField;
	}

	public TextField getBookCopiesTextField() {
		return bookCopiesTextField;
	}
}
